package com.learn.hd.interview.lock;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.io.IOException;

/**
 * @author zhanghaidong
 * @version v1.0
 * @description
 * @date 2019/7/19
 */
public class RedissionConfig {

    public static void main(String[] args) {
        test();
    }

    public static void test() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://192.168.31.235:6379")
                .setPassword("wtoip_yun")
                .setConnectionPoolSize(10)
                .setConnectionMinimumIdleSize(5)
                .setTimeout(1000)
                .setRetryAttempts(3);

        //集群模式
        config.useClusterServers()
                .addNodeAddress("redis://192.168.31.235:6379")
                .addNodeAddress("redis://192.168.31.235:6378");

        RedissonClient client = Redisson.create(config);
        try {
            String s = client.getConfig().toJSON();
            System.out.println(s);

            RLock anyLock = client.getLock("anyLock");
            anyLock.lock();
            //do something
            anyLock.unlock();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            client.shutdown();
        }
    }
}
